//
//  RegisterViewModel.swift
//  testRxSwift
//
//  Created by 火星人 on 2017/7/26.
//  Copyright © 2017年 火星人. All rights reserved.
//

import UIKit
import RxSwift

class RegisterViewModel {

    let nameValidator: Observable<ValidateResult>
    let pwdValidator: Observable<ValidateResult>
    let repwdValidator: Observable<ValidateResult>
    let buttonValidator: Observable<Bool>
    
    init(input: (
        name: Observable<String>,
        pwd: Observable<String>,
        repwd: Observable<String>
        )) {
        nameValidator = input.name.map({
            value in
            return value != "" ? .ok(message: "验证通过") : .failed(message: "名字为空")
        }).shareReplay(1)
        
        pwdValidator = input.pwd.map({
            value in
            return value != "" ? .ok(message: "验证通过") : .failed(message: "请输入密码")
        }).shareReplay(1)
        
        repwdValidator = Observable.combineLatest(input.pwd, input.repwd){
            pwd, repwd in
            
            if pwd == "" {
                return .failed(message: "请先输入密码")
            }
            
            if repwd.length == 0 {
                return .failed(message: "请再次输入")
            }
            
            if pwd != repwd {
                return .failed(message: "两次输入的密码不一致")
            }
            
            return .ok(message: "验证通过")
        }.shareReplay(1)
        
        buttonValidator = Observable.combineLatest(nameValidator, repwdValidator){
            nameResult, pwdResult in
            
            var ok = false
            switch nameResult {
            case .ok:
                ok = true
                break
            default:
                ok = false
            }
            if ok {
                switch pwdResult {
                case .ok:
                    ok = true
                    break
                default:
                    ok = false
                }
            }
            
            return ok
        }
    }
    
}


enum ValidateResult {
    case ok(message: String)            //验证成功和信息
    case empty                          //输入为空
    case validating                     //验证中
    case failed(message: String)        //验证失败的原因
}

extension ValidateResult: CustomStringConvertible {
    var description: String {
        switch self {
        case let .ok(message):
            return message
        case .empty:
            return "输入为空"
        case .validating:
            return "正在输入..."
        case let .failed(message):
            return message
            
        }
    }
}

struct ValidationColors {
    static let okColor = UIColor(red: 138.0 / 255.0, green: 221.0 / 255.0, blue: 109.0 / 255.0, alpha: 1.0)
    static let errorColor = UIColor.red
}

extension ValidateResult {
    var textColor: UIColor {
        switch self {
        case .ok:
            return ValidationColors.okColor
        case .empty:
            return UIColor.black
        case .validating:
            return UIColor.black
        case .failed:
            return ValidationColors.errorColor
        }
    }
}
